﻿using AutoMapper;
using B.S.BasicData.Domain.Entities;
using B.S.BasicData.Error;
using B.S.BasicData.Infrastructure.InProfaces;
using B.S.BasicData.Write.Api.Application.Command.ShopCommand;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHander.ShopCommandHander
{
    public class ShopCreateCommandHander : IRequestHandler<ShopCreateCommand, ApiResult<Shop>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Shop> ShopRepository;

        public ShopCreateCommandHander(IMapper mapper, IBaseRepository<Shop> shopRepository)
        {
            this.mapper = mapper;
            ShopRepository = shopRepository;
        }

        public Task<ApiResult<Shop>> Handle(ShopCreateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<Shop> result = new ApiResult<Shop>();

            var a = mapper.Map<Shop>(request);

            var list = ShopRepository.Add(a).Result;

            if (list > 0)
            {
                result.Code = ApiEnum.success;
                result.Msg = "添加成功";
            }
            else
            {
                result.Code = ApiEnum.success;
                result.Msg = "添加失败";
            }



            return Task.FromResult(result);
        }
    }
}
